package com.whadee.demo.controller;

import com.whadee.demo.domain.dto.UserDTO;
import com.whadee.demo.domain.dto.UserRegisterDTO;
import com.whadee.demo.domain.dto.UserLoginDTO;
import com.whadee.demo.domain.query.UserPageQuery;
import com.whadee.demo.exception.BusinessException;
import com.whadee.demo.result.ResponseEnum;
import com.whadee.demo.result.ResponseVo;
import com.whadee.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 用户模块
 */
@Slf4j
@RestController
@RequestMapping("/api/user")

public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param userRegisterDTO 用户注册信息
     * @return
     */
    @PostMapping("/register")
    public ResponseVo register(@RequestBody UserRegisterDTO userRegisterDTO) {
        ResponseVo responseVo=userService.register(userRegisterDTO);
        return responseVo;
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseVo login(@RequestBody UserLoginDTO userloginDTO){
        ResponseVo  responseVo=userService.login(userloginDTO);
        return responseVo;
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public ResponseVo queryUserPage(  UserPageQuery pageQuery){
        return userService.queryUserPage(pageQuery);
    }
    /**
     * 获取用户列表
     * @return 用户列表
     */
    @GetMapping("/getUsers") //只能添加在方法上，表示接收get请求（查找请求）
    public ResponseVo getUsers() {
        String user1 = "name:" + "张三" + ",Age:" + "18";//模拟数据
        String user2 = "name:" + "李四" + ",Age:" + "19";
        HashMap<String, Object> map = new HashMap<>();
        map.put("1",user1);
        map.put("2",user2);
        log.info("info级别的日志");
        log.debug("debug级别的日志");
        log.error("error级别的日志");
        log.warn("warn级别的日志");
        return ResponseVo.ok(map);
    }

//        if (map == null) {
//            throw new BusinessException(ResponseEnum.ERROR);
//
//        }
//        return ResponseVo.setResult(ResponseEnum.SUCCESS);
//    }
    /**
     *根据姓名和年龄查找用户   不太建议使用
     * @param name 姓名
     * @param age 年龄
     * @return 用户信息
     */
    @GetMapping("/getUserByNameAndAge")
    public ResponseEntity<String> getUserByNameAndAge(
            @RequestParam(name = "name", required = false, defaultValue = "Unknown") String name,
            @RequestParam(name = "age", required = false, defaultValue = "0") int age) {
    /*
    用于解析URL查询字符串参数，例子中你可以通过http://localhost:8080/api/user/getUserByNameAndAge?name=John&age=30
    来访问这个端点，并传递name和age参数
    */
        String response = "Name: " + name + ", Age: " + age;
        return ResponseEntity.ok(response);
    }

    /**
     * 根据id查找用户
     * @param id
     * @return 用户信息
     */
    @GetMapping("/getUserById/{id}")
    public ResponseEntity<String> getUserById(@PathVariable Long id) {
        // @PathVariable 用于接收URL路径参数，http://localhost:8080/api/user/getUserById/123
        String response = "User with ID: " + id;
        return ResponseEntity.ok(response);
    }

    @GetMapping("/users/{id}")
    public ResponseEntity<String> getUserByIdandName(@PathVariable String id, @RequestParam(required = false) String name) {
        String response = "User with ID: " + id + " and name: " + name;
        return ResponseEntity.ok(response);
    }

    /**
     * 保存用户
     * @param user 用户信息
     * @return
     */
    @PostMapping("/save")//用于接收保存数据请求
    public ResponseEntity<UserDTO> save(@RequestBody UserDTO user){
        //对数据进行保存
        return ResponseEntity.ok(user);
    }

    @PutMapping("/updateById")//用于接收更新请求
    public ResponseEntity<UserDTO> updateById(@RequestBody UserDTO user) {
        //userService.updateById(user)
        return ResponseEntity.ok(user);
    }

    @DeleteMapping("/remove/{id}")
    public ResponseEntity<UserDTO> removeById(@PathVariable String id) {
        //UserService.removeById(id)
        UserDTO userDTO = new UserDTO();
        userDTO.setId(id);
        userDTO.setName("张三");
        userDTO.setAge(18);
        userDTO.setEmail("12345645@163.com");
        return ResponseEntity.ok(userDTO);
    }
}

